<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class BonusPoolRecord extends Base
{
    protected $name = 'bonus_pool_record';

    public static $_type = [
        1 => '店主分红池',
        2 => '盟主分红池',
        3 => '区代分红池',
        4 => '市代分红池'
    ];

    public function getTypeTextAttr($value, $data): string
    {
        return isset(self::$_type[$data['type']]) ? self::$_type[$data['type']] : '-';
    }

    public function orders()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id')->field('id,sn');
    }

    /**
     * 分红池入池记录：用户确认收货7天后入池
     * @param $order
     * @return array
     */
    public static function addBonusPoolRecord($order): array
    {
        $rate['shoper'] = Cache::get('system')['bonus_pool_shoper_rate'];
        $rate['leader'] = Cache::get('system')['bonus_pool_leader_rate'];
        $rate['district'] = Cache::get('system')['bonus_pool_district_rate'];
        $rate['city'] = Cache::get('system')['bonus_pool_city_rate'];
        try {
            Db::transaction(function () use ($order, $rate) {
                $model = new BonusPoolRecord();
                $shoper_amount = roundNum($order->profit * $rate['shoper'], 2);
                $leader_amount = roundNum($order->profit * $rate['leader'], 2);
                $district_amount = roundNum($order->profit * $rate['district'], 2);
                $city_amount = roundNum($order->profit * $rate['city'], 2);
                $datas = [
                    [
                        'type' => 1,
                        'profit_rate' => $rate['shoper'],
                        'amount' => $shoper_amount,
                    ]
                ];
                $datas[4] = $datas[3] = $datas[2] = $datas[1] = $datas[0];

                $datas[0]['pool_id'] = 1;
                $datas[1]['pool_id'] = 2;
                $datas[2]['pool_id'] = 3;
                $datas[3]['pool_id'] = 4;
                $datas[4]['pool_id'] = 5;
                $datas = array_merge($datas,[
                    [
                        'type' => 2,
                        'profit_rate' => $rate['leader'],
                        'amount' => $leader_amount,
                        'pool_id' => 6
                    ],[
                        'type' => 3,
                        'profit_rate' => $rate['district'],
                        'amount' => $district_amount,
                        'pool_id' => 7
                    ],[
                        'type' => 4,
                        'profit_rate' => $rate['city'],
                        'amount' => $city_amount,
                        'pool_id' => 8
                    ]
                ]);

                $add = [
                    'order_id' => $order->id,
                    'total_profit' => $order->profit,
                ];
                array_walk($datas, function (&$value, $key, $add) {
                    $value = array_merge($value, $add);
                }, $add);

                $model->insertAll($datas);

                BonusPool::where('type',1)->inc('total_amount', floatval($shoper_amount))->update();
                BonusPool::where('type',2)->inc('total_amount', floatval($leader_amount))->update();
                BonusPool::where('type',3)->inc('total_amount', floatval($district_amount))->update();
                BonusPool::where('type',4)->inc('total_amount', floatval($city_amount))->update();

                $order->is_pooled = 1;
                $order->save();
            });

        }catch (\Exception $e){
            Log::write("addBonusPoolRecord>>".$e->getMessage());
            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '分红池入池执行成功'];
    }
}
